package com.linln.wtp.controller.withdrawal;

import com.linln.common.enums.RoleEnum;
import com.linln.common.utils.JwtUtil;
import com.linln.common.utils.SessionUtil;
import com.linln.common.vo.JsonResult;
import com.linln.modules.system.domain.Role;
import com.linln.modules.system.service.RoleService;
import com.linln.modules.system.service.UserService;
import com.linln.modules.withdrawal.domain.Withdrawal;
import com.linln.modules.withdrawal.service.WithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.wtp.controller.withdrawal
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.wtp.controller.withdrawal
 * @Description: TODO
 * @date Date : 2020年12月30日 15:14
 */

@RestController
@RequestMapping("withdrawal")
public class WithdrawalController {

    @Autowired
    private WithdrawalService withdrawalService;

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Map<String, Object> getWithdrawalList(@RequestParam(required = false, defaultValue = "0", value = "pageNum") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer limit, @RequestParam String shopId, Integer status) {
        return withdrawalService.getWithdrawal(pageNum, limit, shopId, status);
    }

    @PostMapping("/applyWithdrawal")
    public JsonResult addWithdrawal(@RequestParam String shopId, @RequestParam Double money) {
        Long userId = SessionUtil.getUserId();
        Set<Role> roleSet = roleService.getUserOkRoleList(userId);
        boolean flag = true;
        for(Role role : roleSet){
            Long id = role.getId();
            if(RoleEnum.PM.getCode().equals(id)){
                flag = false;
                break;
            }
        }
        if(flag){
            return JsonResult.error("您不是店长不能操作提现");
        }
        if (money <= 0) {
            return JsonResult.error("金额必须大于0");
        }
        return withdrawalService.applyWithdrawal(shopId, money);
//        return JsonResult.error();
    }

    @Value("${server.ip}")
    private String ip;

    @GetMapping("/withdrawalInfo")
    public JsonResult getWithdrawalInfo(@RequestParam("withdrawalId") String withdrawalId) {
        Withdrawal withdrawal = withdrawalService.getWithdrawalInfo(withdrawalId);
        withdrawal.setTransferCredential(ip + withdrawal.getTransferCredential());
        return JsonResult.ok().put("data", withdrawal);
    }

}
